package cn.iocoder.boot.springai001.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 聊天会话实体（JPA版本）
 * <p>
 * 用于将会话信息持久化到数据库
 * <p>
 * 与 ChatMemory 的关系：
 * - ChatMemory 存储消息内容（可以用 JDBC ChatMemoryRepository）
 * - ChatSessionEntity 存储会话元数据（用 Spring Data JPA）
 */
@Entity
@Table(name = "chat_sessions", indexes = {
        @Index(name = "idx_user_id", columnList = "user_id"),
        @Index(name = "idx_user_type", columnList = "user_id,session_type"),
        @Index(name = "idx_last_active", columnList = "last_active_at")
})
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ChatSessionEntity {

    /**
     * 会话ID（主键）
     */
    @Id
    @Column(name = "session_id", length = 100)
    private String sessionId;

    /**
     * 用户ID
     */
    @Column(name = "user_id", length = 50, nullable = false)
    private String userId;

    /**
     * 会话名称
     */
    @Column(name = "session_name", length = 200)
    private String sessionName;

    /**
     * conversationId（用于 ChatMemory）
     * 通常与 sessionId 相同
     */
    @Column(name = "conversation_id", length = 100, unique = true)
    private String conversationId;

    /**
     * 会话类型
     */
    @Column(name = "session_type", length = 50)
    private String sessionType;

    /**
     * 创建时间
     */
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;

    /**
     * 最后活跃时间
     */
    @Column(name = "last_active_at")
    private LocalDateTime lastActiveAt;

    /**
     * 是否置顶
     */
    @Column(name = "pinned")
    private Boolean pinned;

    /**
     * 备注
     */
    @Column(name = "remark", length = 500)
    private String remark;

    /**
     * 是否删除（软删除）
     */
    @Column(name = "deleted")
    private Boolean deleted;
}

